package com.dkm.ec.activity.repository.impl;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.db.persistence.dao.IQueryDao;
import com.dkm.biz.repository.AbstractRepository;
import com.dkm.ec.activity.domain.LimitBuyActivityPartin;
import com.dkm.ec.activity.persistence.dao.LimitBuyActivityPartinQueryDao;
import com.dkm.ec.activity.persistence.entity.LimitBuyActivityPartinPo;
import com.dkm.ec.activity.repository.LimitBuyActivityPartinRepository;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hhq
 * @create 2017/8/14
 */
@Repository
public class LimitBuyActivityPartinRepositoryImpl extends AbstractRepository<String, LimitBuyActivityPartinPo, LimitBuyActivityPartin> implements LimitBuyActivityPartinRepository {

    @Resource
    private LimitBuyActivityPartinQueryDao limitBuyActivityPartinQueryDao;

    @Override
    public LimitBuyActivityPartin newInstance() {
        LimitBuyActivityPartin domain = SpringHelper.getBean(LimitBuyActivityPartin.class);
        domain.setData(new LimitBuyActivityPartinPo());
        return domain;
    }

    @Override
    public LimitBuyActivityPartin newInstance(LimitBuyActivityPartinPo po) {
        LimitBuyActivityPartin domain = SpringHelper.getBean(LimitBuyActivityPartin.class);
        domain.setData(po);
        return domain;
    }

    @Override
    protected IQueryDao<String, LimitBuyActivityPartinPo> getQueryDao() {
        return limitBuyActivityPartinQueryDao;
    }

    @Override
    public List<LimitBuyActivityPartinPo> findActivityGoods(String activityId, int pageIndex, int pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        int startRowIndex = (pageIndex -1) * pageSize;
        int endRowIndex = pageIndex * pageSize;
        params.put("startRowIndex", startRowIndex);
        params.put("endRowIndex", endRowIndex);
        return limitBuyActivityPartinQueryDao.findByKey("findActivityGoods", params);
    }

    @Override
    public Integer countForShopId(String activityId, String shopId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("shopId", shopId);
        return limitBuyActivityPartinQueryDao.countByKey("countForShopId", params);
    }

    @Override
    public Integer getTotalPage(String activityId, int pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        Integer totalRow = limitBuyActivityPartinQueryDao.countByKey("countByActivityId", params);
        int totalPage = (totalRow%pageSize == 0 ? totalRow%pageSize : totalRow%pageSize+1);
        return totalPage;
    }

}
